
package common.packet;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


/**
 * A chat message sent from the client to the server or the opposite.
 *
 * @author xissburg
 */
public class ChatMessage implements Packet
{
    private String message;

    public ChatMessage() 
    {
        this("");
    }

    public ChatMessage(DataInputStream in) throws IOException
    {
        read(in);
    }

    public ChatMessage(String message) {
        this.message = message;
    }

    public ChatMessage(ChatMessage chatMessage) {
        this(chatMessage.getMessage());
    }
    
    public void read(DataInputStream in) throws IOException
    {
        message = in.readUTF();
    }

    public void write(DataOutputStream out) throws IOException
    {
        out.writeUTF(getCode().name());
        out.writeUTF(message);  
    }

    public Code getCode() {
        return Code.CHAT_MESSAGE;
    }

    public ChatMessage copy() {
        return new ChatMessage(this);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return message;
    }
}
